﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;

namespace XNAGame_Lab1
{
    public class Animal : VisibleGame
    {
        int _Dam;

        public int Dam
        {
            get { return _Dam; }
            set { _Dam = value; }
        }
        String _Name;

        public String Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        int _HP;//mau

        public int HP
        {
            get { return _HP; }
            set { _HP = value; }
        }
        int _Def;//suc tan cong

        public int Def
        {
            get { return _Def; }
            set { _Def = value; }
        }
         public Animal(ContentManager content, List<string> strTexture, int nSprites, Vector2 topleft, Vector2 size)
        {
            InitMyXNAButton(content, strTexture, nSprites, ref topleft, ref size);


        }

        private void InitMyXNAButton(ContentManager content, List<string> strTexture, int nSprites, ref Vector2 topleft, ref Vector2 size)
        {
            TopLeft = topleft;
            Size = size;
            nSprites = strTexture.Count;
            Sprites = new List<MySprites2D>();
            Texture2D[] texture = new Texture2D[nSprites];
            for (int i = 0; i < nSprites; i++)
                texture[i] = content.Load<Texture2D>(strTexture[i]);

            MySprites2D mySprite = new MySprites2D(texture, topleft);
            Sprites.Add(mySprite);
            this.nSprites = Sprites.Count;
            
        }
        public Animal(ContentManager content, string refix, int nSprites, Vector2 topleft, Vector2 size)
        {
            InitWithRefix(content, refix, nSprites, ref topleft, ref size);

        }

        private void InitWithRefix(ContentManager content, string refix, int nSprites, ref Vector2 topleft, ref Vector2 size)
        {
            List<string> strTexture = new List<string>(nSprites);
            for (int i = 0; i < nSprites; i++)
                strTexture.Add( refix + (i).ToString("00"));
            InitMyXNAButton(content, strTexture, nSprites, ref topleft, ref size);

        }
        public Animal(ContentManager content, string strXML)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(strXML);
            XmlNodeList root = doc.GetElementsByTagName("buttons");
            XmlNode parent = root.Item(0);
            XmlNode button = parent.ChildNodes.Item(0);
            XmlNode texture = button.ChildNodes.Item(4);
            List<string> strTexture = new List<string>(15);
//            int nSprites = 1;
            Vector2 topleft = new Vector2();
            Vector2 size = new Vector2();
            XmlNode X = button.ChildNodes.Item(0);
            XmlNode Y = button.ChildNodes.Item(1);
            XmlNode W = button.ChildNodes.Item(2);
            XmlNode H = button.ChildNodes.Item(3);
            
            topleft.X =int.Parse( X.InnerText);
            topleft.Y = int.Parse(Y.InnerText);
            size.X = int.Parse(W.InnerText);
            size.Y = int.Parse(H.InnerText);
            
            for(int i =0;i<15;i++)
            {
                XmlNode texture1 = texture.ChildNodes.Item(i);
                strTexture[i] = texture1.InnerText;
                
            }
            InitMyXNAButton(content, strTexture, nSprites, ref topleft, ref size);
//            XmlNodeList button = buttons.("button");


        }
        public virtual void AnimalMove()
        {
            this._TopLeft.X++;
//            if(this._TopLeft.X==)
//            if(this._TopLeft.X == 
        }
        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            base.Draw(gameTime, spriteBatch);
        }
        public override void Update(GameTime gameTime)
        {
           // AnimalMove();
            base.Update(gameTime);
        }
        /// <summary>
        /// ham kiem tra va cham 
        /// </summary>
        public virtual bool isCollide(Animal animal)
        {
            if (TopLeft.X + Size.X == animal._TopLeft.X)
                return true;
            return false;
                
        }
        /// <summary>
        /// event 
        /// </summary>
        /// <returns></returns>
        public bool isDie()
        {
            if (this.HP == 0)
                return true;
            return false;
        }


    }
}
